home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / fractal / kaos.lha / autolib / auto_p_go_proc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-13  |  5.2 KB  |  169 lines

  1. /*
  2. ### Execute an auto executable and pipe the output to the combined space box ###
  3. --------------------------------------------------------------------------------
  4. In case of AUTO86 executables recognized by "@auto", it sets
  5. the region_index to 2, aux_win_mode of the appropriate aux window to 1.
  6. ------------------------------------------------------------------------------*/
  7. #include <stdio.h>
  8.  
  9.  
  10. #include <suntool/sunview.h>
  11. #include <suntool/textsw.h>
  12. #include <suntool/panel.h>
  13. #include <sunwindow/notify.h>
  14. static FILE *fp_auto_fromchild,*fp_auto_tochild;
  15. static int auto_fromchild,auto_tochild;
  16. static int auto_childpid;
  17.  
  18. void
  19. auto_panel_go_proc()
  20. {
  21.     static Notify_value auto_pipe_reader();
  22.     static Notify_value auto_dead_child();
  23.     int pipefrom[2],pipeto[2];
  24.     int c,numfds;
  25.  
  26.     int i;
  27.     extern int auto_option;
  28.     extern int region_index;
  29.     extern char string[],lstring[],auto_dir_name[],auto_file_name[],auto_input_name[];
  30.     extern FILE *popen();
  31.     extern Textsw auto_textsw;
  32.     extern Panel_item auto_panel_go_item,auto_dir_name_item,auto_file_name_item,auto_input_name_item;
  33.     
  34.     all_reset();
  35.  
  36.     /* Scan the executable file name and determine if it is from AUTO86.
  37.     If it is, set region_index to 2 which is necessary for recording the
  38.     data to data2 by record_data() */
  39.     /* WORKING VERSION */
  40.     if(strncmp(auto_file_name,"@auto",5)==0){
  41.         auto_option = 1;  /* default choice, will be changed by
  42.                     get_auto_option */
  43.         region_index = 2;
  44.             sprintf(lstring,"%s/%s",auto_dir_name,auto_file_name);
  45.         system_mess_proc(0,"running AUTO86 preprocessor...");
  46.     /*
  47.             textsw_store_file(auto_textsw,lstring,0,0);
  48.             sprintf(lstring,"%s/%s^%s",auto_dir_name,auto_file_name,auto_dir_name,auto_input_name);
  49.     */
  50.     }
  51.     else {
  52.             sprintf(lstring,"%s/%s",auto_dir_name,auto_file_name);
  53.     }
  54.  
  55.     /* Decode an executable string replacing ^ with a white space */
  56.     if(lstring!= NULL){
  57.         i=0;
  58.         while(lstring[i] != '\0'){
  59.             if(lstring[i] == '^')
  60.                 string[i]=' ';
  61.             else
  62.                 string[i]=lstring[i];
  63.             i++;
  64.         }
  65.         string[i] = '\0';
  66.     }
  67.     strcpy(lstring,string);
  68.  
  69.     /* Create a pipe */
  70.         if(pipe(pipefrom)<0){
  71.                 perror("new process");
  72.         system_mess_proc(1,"Error: Piping failed! Check the file name!");
  73.         return;
  74.     }
  75.     if(pipe(pipeto) < 0){
  76.                 perror("new process");
  77.         system_mess_proc(1,"Error: Piping failed! Check the file name!");
  78.         return;
  79.     }
  80.  
  81.     /* Fork a child */
  82.         switch (auto_childpid = fork()){
  83.                 case -1:
  84.                         perror("new process");
  85.                         exit(1);
  86.                 case 0:
  87.                         /* use dup2 to set the child's stdout to
  88.                         the pipe */
  89.                         dup2(pipeto[0],0);
  90.                         dup2(pipefrom[1],1);
  91.  
  92.                         /* close all other fds (except stderr) since child
  93.                         process doesn't know about or need them */
  94.                         numfds = getdtablesize();
  95.                         for (c = 3; c < numfds; c++)
  96.                                 close(c);
  97.                         (void) execl("/bin/csh","csh","-c",lstring,0);
  98.                         perror("new process: child");
  99.                         exit(1);
  100.                 default:
  101.                         close(pipeto[0]);
  102.                         auto_tochild = pipeto[1];
  103.                         fp_auto_tochild = fdopen(auto_tochild, "w");
  104.                         close(pipefrom[1]);
  105.                         auto_fromchild = pipefrom[0];
  106.                         fp_auto_fromchild = fdopen(auto_fromchild, "r");
  107.  
  108.                         /* The pipe should be unbuffered or "new process" 
  109.                         will not get any data until 1024 characters have
  110.                         been sent */
  111.                         setbuf(fp_auto_tochild,NULL);
  112.                         setbuf(fp_auto_fromchild,NULL);
  113.                         break;
  114.         }
  115.  
  116.     /* Register input and wait3 functions to the notifier */
  117.         (void) notify_set_input_func(auto_panel_go_item,auto_pipe_reader,auto_fromchild);
  118.         (void) notify_set_wait3_func(auto_panel_go_item,auto_dead_child,auto_childpid);
  119.     return;
  120. }
  121.  
  122.  
  123. /*------------------------------------------------------------------------------
  124. Read the input pending on the pipe
  125. ------------------------------------------------------------------------------*/
  126.  
  127.  
  128. #include <stdio.h>
  129. #include <suntool/sunview.h>
  130. #include <suntool/panel.h>
  131. #include <sunwindow/notify.h>
  132.  
  133. static Notify_value
  134. auto_pipe_reader(item,fd)
  135. Panel_item item;
  136. int fd;
  137. {
  138.     extern FILE *fp_auto_fromchild;
  139.     extern char auto_file_name[];
  140.  
  141.     if(strncmp(auto_file_name,"@auto",5)==0){
  142.         auto_load_data(fp_auto_fromchild);
  143.     }
  144.     return(NOTIFY_DONE);
  145. }
  146.  
  147.  
  148. /*------------------------------------------------------------------------------
  149. Close the pipe and let the notifier know if the child process dies
  150. ------------------------------------------------------------------------------*/
  151.  
  152.  
  153. #include <suntool/sunview.h>
  154. #include <suntool/panel.h>
  155. #include <sunwindow/notify.h>
  156.  
  157. static Notify_value
  158. auto_dead_child(item,pid,status,rusage)
  159. Panel_item item;
  160. int     pid;
  161. union wait *status;
  162. struct rusage *rusage;
  163. {
  164.     extern int auto_fromchild;
  165.         (void) notify_set_input_func(item,NOTIFY_FUNC_NULL,auto_fromchild);
  166.         close(auto_fromchild);
  167.         return(NOTIFY_DONE);
  168. }
  169.